import { Engine } from "../engine";  
import { Vector2 } from "../vector";  
import { DynamicBody } from "../physics/physics";  
import { EnemyPhysics } from "./enemy-physics";  
  
export enum EnemyType {  
  WALKER = "walker",  
  FLYER = "flyer",   
  GUARD = "guard"  
}  
  
export enum EnemyState {  
  IDLE = "idle",  
  PATROL = "patrol",  
  CHASE = "chase",  
  ATTACK = "attack",  
  DEAD = "dead"  
}  
  
export interface EnemyAnimation {  
  frame: number;  
  direction: "l" | "r";  
  scale: number;  
  rotation: number;  
}  
  
export abstract class Enemy {  
  body_: DynamicBody;  
  physics: EnemyPhysics;  
  animation_: EnemyAnimation;  
    
  type: EnemyType;  
  state: EnemyState = EnemyState.IDLE;  
  health: number = 100;  
  maxHealth: number = 100;  
  damage: number = 10;  
  speed: number = 1.5;  
  detectionRange: number = 150;  
  attackRange: number = 30;  
    
  lastStateChange: number = 0;  
  lastAttack: number = 0;  
  attackCooldown: number = 1000;  
    
  patrolPoints: Vector2[] = [];  
  currentPatrolIndex: number = 0;  
    
  isDead: boolean = false;  
  
  constructor(public engine: Engine, pos: Vector2, type: EnemyType) {  
    this.type = type;  
    this.createBody(pos);  
    this.physics = new EnemyPhysics(engine.physics, this);  
    this.animation_ = {  
      frame: 0,  
      direction: "r",  
      scale: 1,  
      rotation: 0  
    };  
  }  
  
  abstract update_(): void;  
  abstract render(ctx: CanvasRenderingContext2D): void;  
  
  createBody(pos: Vector2): void {  
    this.body_ = {  
      radius: 8,  
      pos: pos,  
      oldPos: pos.copy(),  
      vel: new Vector2(),  
      contactPoints: []  
    };  
  }  
  
  takeDamage(damage: number): void {  
    if (this.isDead) return;  
      
    this.health -= damage;  
    if (this.health <= 0) {  
      this.die();  
    }  
  }  
  
  die(): void {  
    this.isDead = true;  
    this.state = EnemyState.DEAD;  
      
    // 生成死亡粒子效果  
    this.engine.particles.emit({  
      count: 50,  
      direction_: new Vector2(3, 0),  
      lifetime: 100,  
      pos: this.body_.pos  
    });  
  }  
  
  getDistanceToPlayer(): number {  
    return this.body_.pos.distanceTo(this.engine.player.body_.pos);  
  }  
  
  canSeePlayer(): boolean {  
    const distance = this.getDistanceToPlayer();  
    return distance <= this.detectionRange && !this.engine.player.isDead;  
  }  
  
  moveTowards(target: Vector2): void {  
    const direction = target.copy().sub_(this.body_.pos).normalize_();  
    this.body_.vel.x = direction.x * this.speed;  
    this.animation_.direction = direction.x > 0 ? "r" : "l";  
  }  
  
  patrol(): void {  
    if (this.patrolPoints.length === 0) return;  
      
    const target = this.patrolPoints[this.currentPatrolIndex];  
    const distance = this.body_.pos.distanceTo(target);  
      
    if (distance < 20) {  
      this.currentPatrolIndex = (this.currentPatrolIndex + 1) % this.patrolPoints.length;  
    } else {  
      this.moveTowards(target);  
    }  
  }  
  
  attackPlayer(): void {  
    const now = this.engine.time_;  
    if (now - this.lastAttack < this.attackCooldown) return;  
      
    const distance = this.getDistanceToPlayer();  
    if (distance <= this.attackRange) {  
      // 对玩家造成伤害  
      this.engine.player.die(); // 简化实现，直接杀死玩家  
      this.lastAttack = now;  
    }  
  }  
}